home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 351-375 / 351 / pdc / pdcsrc.lzh / PDC / Symbol.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  4KB  |  173 lines

  1.  
  2. /* PDC Compiler - A Freely Distributable C Compiler for the Amiga
  3.  *                Based upon prior work by Matthew Brandt and Jeff Lydiatt.
  4.  *
  5.  * PDC Compiler release 3.3 Copyright (C) 1989 Paul Petersen and Lionel Hummel.
  6.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  7.  *
  8.  * This code is freely redistributable upon the conditions that this 
  9.  * notice remains intact and that modified versions of this file not be 
  10.  * distributed as part of the PDC Software Distribution without the express
  11.  * consent of the copyright holders.
  12.  *
  13.  *------------------------------------------------------------------
  14.  *
  15.  * $Log:    Symbol.c,v $
  16.  * Revision 3.33  90/04/05  22:47:07  lionel
  17.  * None.
  18.  * 
  19.  * Revision 3.32  90/02/03  16:25:53  lionel
  20.  * None
  21.  * 
  22.  *------------------------------------------------------------------
  23.  */
  24.  
  25. /*
  26.  * Symbol.c
  27.  * 
  28.  * Routines for managing the symbol table.
  29.  */
  30.  
  31. #include    <stdio.h>
  32. #include    "C.h"
  33. #include    "Expr.h"
  34. #include    "Gen.h"
  35. #include    "Cglbdec.h"
  36.  
  37. extern TABLE    gsyms;
  38. extern TABLE    lsyms;
  39.  
  40. #ifdef SLOW_BUT_SURE
  41.  
  42. int
  43. hashkey(str)            /* Compute the 16 bit CRC of the name */
  44.     char           *str;
  45. {
  46.     int             key = 0;
  47.     int             val;
  48.     char           *ptr;
  49.  
  50.     if (str == NULL)
  51.         return (0);
  52.  
  53.     for (ptr = str; *ptr; ++ptr) {
  54.         val = ((key >> 8) ^ (*ptr)) & 0xFF;
  55.         val = val ^ (val >> 4);
  56.         key = 0xFFFF & ((key << 8) ^ val ^ (val << 5) ^ (val << 12));
  57.     }
  58.     return (key);
  59. }
  60.  
  61. #else
  62.  
  63. int
  64. hashkey(str)            /* Compute a sliding XOR of the name */
  65.     char           *str;
  66. {
  67.     register int    key = 0;
  68.     register char  *ptr;
  69.  
  70.     if (str == NULL)
  71.         return (0);
  72.  
  73.     for (ptr = str; *ptr; ++ptr)
  74.         key = (key << 1) ^ (*ptr);
  75.  
  76.     return (key);
  77. }
  78.  
  79. #endif
  80.  
  81. SYM            *
  82. search(str, ptr)
  83.     register char  *str;
  84.     register SYM   *ptr;
  85. {
  86.     register int    newkey;
  87.     register SYM   *answer = NULL;
  88.  
  89.     newkey = hashkey(str);
  90.  
  91.     while (ptr != NULL) {
  92.         if (ptr->key == newkey)
  93.             if (ptr->name != NULL && strcmp(str, ptr->name) == 0)
  94.                 answer = ptr;
  95.         ptr = ptr->next;
  96.     }
  97.     return answer;
  98. }
  99.  
  100. SYM            *
  101. gsearch(str)
  102.     char           *str;
  103. {
  104.     SYM            *ptr;
  105.  
  106.  
  107.     if ((ptr = search(str, &lsyms.head)) == NULL)
  108.         ptr = search(str, &gsyms.head);
  109.     return (ptr);
  110. }
  111.  
  112. /* Insert the SYM into the TABLE, Add to end of the table    */
  113.  
  114. void
  115. insert(sp, table)
  116.     SYM            *sp;
  117.     TABLE          *table;
  118. {
  119.     sp->key = hashkey(sp->name);
  120.  
  121.     if (table != NULL && sp != NULL) {
  122.         sp->next = NULL;
  123.         if (table->head == NULL)
  124.             table->head = sp;
  125.         else
  126.             table->tail->next = sp;
  127.         table->tail = sp;
  128.     }
  129. }
  130.  
  131. /* Remove the string sp from table */
  132.  
  133. void
  134. remove(s, table)
  135.     char           *s;
  136.     TABLE          *table;
  137. {
  138.     register int    newkey;
  139.     register SYM   *ptr, *last;
  140.  
  141.     if (table != NULL && s != NULL) {
  142.  
  143.         newkey = hashkey(s);
  144.  
  145.         ptr = table->head;
  146.  
  147.         if (ptr != NULL && ptr->key == newkey) {
  148.             if (ptr->name != NULL && strcmp(s, ptr->name) == 0) {
  149.                 table->head = ptr->next;
  150.                 if (table->head == NULL)
  151.                     table->tail = NULL;
  152.                 return;
  153.             }
  154.         }
  155.  
  156.         last = ptr;
  157.         ptr = ptr->next;
  158.  
  159.         while (ptr != NULL) {
  160.             if (ptr->key == newkey) {
  161.                 if (ptr->name != NULL && strcmp(s, ptr->name) == 0) {
  162.                     last->next = ptr->next;
  163.                     if (table->tail == ptr)
  164.                         table->tail = last;
  165.                     return;
  166.                 }
  167.             }
  168.             last = ptr;
  169.             ptr = ptr->next;
  170.         }
  171.     }
  172. }
  173.